﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CameraParentClassLibrary;

namespace _01_多相机_框架
{
    public partial class Form1 : Form
    {
        List<LibraryInfo> libraryInfos = new List<LibraryInfo>();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            //获取指定路径下的所有动态链接库
            string[] dllFiles = System.IO.Directory.GetFiles("./Camera", "*.dll");
            foreach (string item in dllFiles)
            {
                //加载动态链接库
                System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFrom(item);
                //遍历动态链接库中的所有类，只需要从CameraParentClassLibrary.CameraParent派生出来的子类
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.BaseType.FullName == "CameraParentClassLibrary.CameraParent")//判断父类是否为相机父类
                    {
                        //实例化记录动态链接库的信息
                        LibraryInfo libraryInfo = new LibraryInfo();
                        //相机品牌名称
                        libraryInfo.BrandName = ((CameraParent)Activator.CreateInstance(type)).BrandName;
                        //动态链接库的路径
                        libraryInfo.DllFullName = item;
                        //类的全名
                        libraryInfo.ClassFullName = type.FullName;
                        //添加到集合当中
                        libraryInfos.Add(libraryInfo);
                        //添加到下拉列表当中
                        comboBox1.Items.Add(libraryInfo.BrandName);
                    }
                }
            }
            //判断下拉框有内容的情况下默认选择第一个
            if (comboBox1.Items.Count > 0) comboBox1.SelectedIndex = 0;
            //批量绑定方法
            foreach (Button item in this.groupBox3.Controls)
            {
                if (item.Text == "隐藏") item.Click += (S, E) => cameraParents.ForEach(t => t.isShowBtn(false));
                if (item.Text == "显示") item.Click += (S, E) => cameraParents.ForEach(t => t.isShowBtn(true));
                if (item.Text == "打开相机") item.Click += (S, E) => cameraParents.ForEach(t => t.OpenCamera());
                if (item.Text == "关闭相机") item.Click += (S, E) => cameraParents.ForEach(t => t.CloseCamera());
                if (item.Text == "采集一张") item.Click += (S, E) => cameraParents.ForEach(t => t.CollectOne());
                if (item.Text == "连续采集") item.Click += (S, E) => cameraParents.ForEach(t => t.CollectOneContinuous());
            }
            //批量绑定方法
            foreach (Control item in this.groupBox2.Controls)
            {
                if (item is Button btn)
                {
                    if (btn.Text == "打开相机") btn.Click += (S, E) => cameraParent?.OpenCamera();
                    if (btn.Text == "关闭相机") btn.Click += (S, E) => cameraParent?.CloseCamera();
                    if (btn.Text == "采集一张") btn.Click += (S, E) => cameraParent?.CollectOne();
                    if (btn.Text == "连续采集") btn.Click += (S, E) => cameraParent?.CollectOneContinuous();
                }
            }
        }

        /// <summary>
        /// 相机编号
        /// </summary>
        int CameraInex = 0;
        
        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //添加相机的时候限制最多只能添加六个相机
            if (CameraInex >= 6)
            {
                MessageBox.Show("最多只能添加六个相机");
                return;
            }
            this.listBox1.Items.Add("相机-" + (++CameraInex) + "|" + this.comboBox1.SelectedItem.ToString());
                
        }
        List<CameraParent> cameraParents = new List<CameraParent>();
        /// <summary>
        /// 初始化相机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //清空容器
            this.panel1.Controls.Clear();
            cameraParents.Clear();
            this.comboBox2.Items.Clear();

            //创建布局器
            TableLayoutPanel flowLayoutPanel = null;
            //获取相机数量
            int count = this.listBox1.Items.Count;
            //根据不同的情况创建布局器当中的内容
            switch (count)
            {
                case 1:
                    //创建布局器
                    flowLayoutPanel = TableLayoutPanelClass.CreateTableLayoutPanelByPercent(new float[] { 100F }, new float[] { 100F });
                    //创建相机控件
                    CameraControl cameraControl = CreateCameraControlByListBoxItem(this.listBox1.Items[0].ToString());
                    //添加相机控件进入布局器当中
                    flowLayoutPanel.Controls.Add(cameraControl,0,0);
                    //添加布局器到容器当中
                    this.panel1.Controls.Add(flowLayoutPanel);
                    break;
                case 2:
                    //创建布局器
                    flowLayoutPanel = TableLayoutPanelClass.CreateTableLayoutPanelByPercent(new float[] { 50F,50F }, new float[] { 100F });
                    //创建相机控件
                    CameraControl cameraControl1 = CreateCameraControlByListBoxItem(this.listBox1.Items[0].ToString());
                    CameraControl cameraControl2 = CreateCameraControlByListBoxItem(this.listBox1.Items[1].ToString());
                    //添加相机控件进入布局器当中
                    flowLayoutPanel.Controls.Add(cameraControl1, 0, 0);
                    flowLayoutPanel.Controls.Add(cameraControl2, 1, 0);
                    //添加布局器到容器当中
                    this.panel1.Controls.Add(flowLayoutPanel);
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
            }
            //添加到的布局器到容器当中
            this.panel1.Controls.Add(flowLayoutPanel);
        }

        public CameraControl CreateCameraControlByListBoxItem(string listBoxItem)
        {
            //获取相机项目与品牌名称
            string[] str = listBoxItem.ToString().Split('|');
            //根据品牌名称获取对应的动态链接库信息
            LibraryInfo libraryInfo = libraryInfos.FirstOrDefault(p => p.BrandName == str[1]);
            //加载动态链接库并且实例化
            CameraParent cameraParent = (CameraParent)Assembly.LoadFrom(libraryInfo.DllFullName).CreateInstance(libraryInfo.ClassFullName);
            //实例化相机控件然后填入容器
            CameraControl cameraControl = new CameraControl(cameraParent) { Dock = DockStyle.Fill };
            //设置相机名称
            cameraControl.SetCameraName(str[0]);
            //添加到下拉列表当中
            this.comboBox2.Items.Add(str[0]);
            //添加到相机集合当中
            cameraParents.Add(cameraParent);
            //返回控件对象
            return cameraControl;

        }

        CameraParent cameraParent=null;
        /// <summary>
        /// 当在下拉框中选择的对象改变的时候
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取下拉框选中的相机名称
            string cameraName = this.comboBox2.SelectedItem.ToString();
            //获取相机对象
            cameraParent = cameraParents.FirstOrDefault(p => p.CameraName == cameraName);
        }
    }
}
